<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
function successMsg($msg){
    return ['code' => 200,'msg' => $msg];
}
function errorMsg($msg){
    return ['code' => -1,'msg' => $msg];
}

// 应用公共文件
function getMenuByPid($pid)
{
    $data = Db::table("ad_menu")->where("pid", $pid)->select();
    $res  = [];
    //遍历 递归
    foreach ($data as $key => $value) {
        // 代码段：核心点
        $value['menu'] = getMenuByPid($value['id']);
        $res[]         = $value;
    }
    return $res;
}

/**
 * 图片上传
 * @param $file
 * @return string|null
 */
function fileImage($file){
    $file = request()->file($file);
    if(empty($file)) return null;
    $info = $file->move( './static/uploads/');
    $save_name = $info->getSaveName();
    $path = str_replace('\\','/',$save_name);
    return '/static/uploads/'.$path;
}

/**
 * 修改图片
 * @param array $param
 * @param array $old_data
 * @param $key
 * @return mixed|string|null
 */
function editImage(array $param,array $old_data,$key)
{
    if(isset($param[$key])){
        $result = $old_data[$key];
    }else{
        $result = fileImage($key);
    }
    return $result;
}

/**
 * 删除图片
 * @param $path
 * @return bool
 */
function fileDel($path)
{
    if(file_exists($path)){
        unlink($path);
        return true;
    }else{
        return true;
    }
}

//递归查询出来分类
function getList($brr, $pid = 0, &$arr = [], $level = 1)
{
    foreach ($brr as $v) {
        if ($v['pid'] == $pid) {
            $v['level'] = $level;
            $arr[]      = $v;
            getList($brr, $v['cate_id'], $arr, $level + 1);
        }
    }
    return $arr;
}